package controller;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import interceptor.LoginCheck;
import model.Post;

import java.util.List;

/**
 * Created by Administrator on 2018/6/6 0006.
 */

public class ShowDetial extends Controller {
    @ActionKey("/detial")
    public void showdetial(){
        String topicid = getPara("topicid");
        setAttr("topic_id",topicid);
        List<Record> topic = Db.find("SELECT topic.id,topic.`name`,topic.content,topic.pub_date,topic.deleted,topic.category_id,category.`name` AS cname FROM topic INNER JOIN category ON topic.category_id = category.id WHERE topic.id="+topicid);
        setAttr("topic",topic);
        setAttr("nickname",getSessionAttr("nickname"));
        List<Record> category = Db.find("select * from category where deleted =0");
        setAttr("category",category);
       // List<Record> post = Db.find("SELECT post.content,post.pub_date,`user`.nickname FROM post INNER JOIN `user` ON post.user_id = `user`.id where topic_id="+topicid+" ORDER BY post.pub_date DESC ");
        Page<Post> post = Post.dao.paginate(1,25,"SELECT post.content,post.pub_date,`user`.nickname","FROM post INNER JOIN `user` ON post.user_id = `user`.id where topic_id="+topicid+" ORDER BY post.pub_date DESC");
        setAttr("post",post);
        renderFreeMarker("/template/detail.ftl");

    }
    @Before(LoginCheck.class)
    @ActionKey("/detail/post")
    public void post(){
        String topicid = getPara();
        int  userid = getSessionAttr("userid");
        String content = getPara("content");
        System.out.println(topicid+userid+content);
        Record post = new Record().set("user_id",userid).set("topic_id",topicid).set("content",content).set("deleted",0);
        Db.save("post",post);
        redirect("http://127.0.0.1:8080/detial?topicid="+topicid);

    }
}
